const birdDom = document.querySelector(".bird");
const gameDom = document.querySelector(".game");
const birdStyleData = getComputedStyle(birdDom);
const gameStyleData = getComputedStyle(gameDom);

const birdWidth = parseFloat(birdStyleData.width);
const birdHeight = parseFloat(birdStyleData.height);
const birdTop = parseFloat(birdStyleData.top);
const birdLeft = parseFloat(birdStyleData.left);

const gameHeight = parseFloat(gameStyleData.height);

class Bird extends Rectangle {
  constructor() {
    super(birdWidth, birdHeight, birdLeft, birdTop, 0, 0, birdDom);
    this.g = 1500;
    this.maxMoveY = gameHeight - (birdHeight + landHeight);
    this.birdSwingStatus = 1;
    this.birdSwingStatusTimer = null;
    this.render();
  }
  move(duration) {
    super.move(duration);
    // 下落的速度公式
    this.ySpeed += this.g * duration;
  }
  // 设置移最大/最小范围值
  onMove() {
    if (this.top < 0) {
      this.top = 0;
    }
    if (this.top > this.maxMoveY) {
      this.top = this.maxMoveY;
    }
  }
  // 小鸟跳跃
  jump() {
    this.ySpeed = -400;
  }

  render() {
    // 复用父类渲染逻辑
    super.render();
    this.dom.className = `bird swing${this.birdSwingStatus}`;
  }

  // 翅膀煽动
  startSwing() {
    if (this.birdSwingStatusTimer) {
      return;
    }
    this.birdSwingStatusTimer = setInterval(() => {
      this.birdSwingStatus = ((this.birdSwingStatus + 1) % 3) + 1;
    }, 200);
  }

  stopSwing() {
    clearInterval(this.birdSwingStatusTimer);
    this.birdSwingStatusTimer = null;
  }
}
